package framework.sinkers;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import framework.Constants;

/**
 * The measurements formatter applicable for systems A, B and C
 * All the systems should write each the same type of measurements in the same way .
 */
public class SystemsABCFormatter implements MeasureFormatter {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy:dd:hh:mm:ss");
    DecimalFormat dfAltitude = new DecimalFormat("000000.00000");
    DecimalFormat dfPressure = new DecimalFormat("00.00000");
    DecimalFormat dfTemperature = new DecimalFormat("000.00000");
    DecimalFormat dfAttitude = new DecimalFormat("000.00000");

    /**
     *
     * @param id    The ID that is being formatted
     * @param measure Measurement will be formatted according to its ID
     * @param extrapolated True if the value is a wildoint, replaced by an extrapolation. An * will be added to the output.
     * @return
     */
    public String format(int id, long measure, boolean extrapolated) {
        String formatted = "";
        switch(id) {
            case Constants.TIME_ID: // timestamp
                formatted  = sdf.format(new Date(measure));
                break;
            case Constants.ALTITUDE_ID: // altitude
                formatted  = dfAltitude.format(Double.longBitsToDouble(measure));
                break;
            case Constants.PRESSURE_ID: // pressure
                formatted  = dfPressure.format(Double.longBitsToDouble(measure));
                formatted = formatted.replaceAll("[,\\.]", ":");
                break;
            case Constants.TEMPERATURE_ID: // temperature
                formatted  = dfTemperature.format(Double.longBitsToDouble(measure));
                break;
            case Constants.ATTITUDE_ID: // attitude
                formatted  = dfAttitude.format(Double.longBitsToDouble(measure));
                break;
            default:
                return formatted;
        }

        if (extrapolated) {
            return formatted+"*";
        } else {
            return formatted;
        }

    }
}
